/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 26727
 * Date: 2024-04-14
 * Time: 20:24
 */
// 467.环绕字符串唯一的子字符串
class Solution8 {
    public int findSubstringInWraproundString(String s) {
        int n = s.length();
        char[] arr = s.toCharArray();
        //1.创建dp表
        //dp[i]表示以i结尾的子数组在base中出现的次数
        int[] dp = new int[n];
        //2.初始化
        for(int i = 0; i < n; i++) {
            dp[i] = 1;
        }
        //3.状态转移
        //4.填表顺序
        for(int i = 1; i < n; i++) {
            if(arr[i-1]+1 == arr[i] || (arr[i-1] == 'z' && arr[i] == 'a')) {
                dp[i] += dp[i-1];
            }
        }
        //5.返回值
        //去重
        int sum = 0;
        int[] ret = new int[26];
        for(int i = 0; i < n; i++) {
            ret[arr[i]-'a'] = Math.max(dp[i],ret[arr[i]-'a']);
        }
        for(int x : ret) {
            sum += x;
        }
        return sum;
    }
}
